package cn.edu.szh.view.baseUI;

import cn.edu.szh.bean.StudentInfo;
import cn.edu.szh.dao.StudentDao;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

public class StudentInfoUI extends JFrame {
    JScrollPane scrollPane = new JScrollPane();
    JTextField textFieldGrade_id = new JTextField();//年级id
    JTextField textFieldClass_id = new JTextField();//班级id
    JTextField textFieldStudent_id = new JTextField(); //学号
    JTextField textFieldName = new JTextField();// 姓名
    JTextField textFieldSex = new JTextField(); //性别
    JTextField textFieldCollege = new JTextField(); //学院
    JTextField textFieldMajor = new JTextField(); //专业
    JTextField textFieldIntime = new JTextField(); //入学日期
    JTextField textFieldId_num = new JTextField(); //身份证
    JTextField textFieldStatus = new JTextField();//状态

    AbstractTableModel tableModel ;
    String[] columnNames = {"年级id", "班级id", "学号", "姓名", "性别", "学院", "专业", "入学日期","身份证" ,"状态"};
    StudentDao studentDao = new StudentDao();
    Vector vector= new Vector();
    public StudentInfoUI()  {
        this.setTitle("学生信息");
        this.setSize(800, 800);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        setLayout(new BorderLayout());

        JPanel panelUP = new JPanel();
        JPanel panelMid = new JPanel();
        JPanel panelDown = new JPanel();
        add(panelUP, BorderLayout.NORTH);
        add(panelMid, BorderLayout.CENTER);
        add(panelDown, BorderLayout.SOUTH);
        // JScrollPane scrollPane = new JScrollPane();
        panelUP.add(scrollPane);
        createTable();

        panelMid.add(new JLabel("年级id"));
        textFieldGrade_id.setColumns(10);
        panelMid.add(textFieldGrade_id);

        panelMid.add(new JLabel("班级id"));
        textFieldClass_id.setColumns(10);
        panelMid.add(textFieldClass_id);

        panelMid.add(new JLabel("学号"));
        textFieldStudent_id.setColumns(10);
        panelMid.add(textFieldStudent_id);

        panelMid.add(new JLabel("姓名"));
        textFieldName.setColumns(10);
        panelMid.add(textFieldName);

        panelMid.add(new JLabel("性别"));
        textFieldSex.setColumns(10);
        panelMid.add(textFieldSex);

        panelMid.add(new JLabel("学院"));
        textFieldCollege.setColumns(10);
        panelMid.add(textFieldCollege);

        panelMid.add(new JLabel("专业"));
        textFieldMajor.setColumns(10);
        panelMid.add(textFieldMajor);

        panelMid.add(new JLabel("入学日期"));
        textFieldIntime.setColumns(10);
        panelMid.add(textFieldIntime);

        panelMid.add(new JLabel("身份证"));
        textFieldId_num.setColumns(10);
        panelMid.add(textFieldId_num);

        panelMid.add(new JLabel("状态"));
        textFieldStatus.setColumns(10);
        panelMid.add(textFieldStatus);

        JButton buttonAdd = new JButton("添加");
        panelDown.add(buttonAdd);
        buttonAdd.addActionListener(e -> {
            addStudent();
            selectStudent();
            reset();
        });

        JButton buttonUpdate = new JButton("修改");
        panelDown.add(buttonUpdate);
        buttonUpdate.addActionListener(e -> {
            updateStudent();
            selectStudent();
            reset();
        });

        JButton buttonDelete = new JButton("删除");
        panelDown.add(buttonDelete);
        buttonDelete.addActionListener(e -> {
            deleteStudent();
            selectStudent();
            reset();
        });

        JButton buttonReset = new JButton("重置");
        panelDown.add(buttonReset);
        buttonReset.addActionListener(e -> {
            reset();
            selectStudent();
        });

        selectStudent();
    }

    private void addStudent() {
        StudentInfo  studentInfo = new StudentInfo(); // 创建学生对象
        studentInfo.setGrade_id(Integer.parseInt(textFieldGrade_id.getText()));
        studentInfo.setClass_id(Integer.parseInt(textFieldClass_id.getText()));
        studentInfo.setStudent_id(Integer.parseInt(textFieldStudent_id.getText()));
        studentInfo.setName(textFieldName.getText());
        studentInfo.setSex(textFieldSex.getText());
        studentInfo.setCollege(textFieldCollege.getText());
        studentInfo.setMajor(textFieldMajor.getText());
        studentInfo.setIntime(Integer.parseInt(textFieldIntime.getText()));
        studentInfo.setId_num(textFieldId_num.getText());
        studentInfo.setStatus(textFieldStatus.getText());
        if(StudentDao.add(studentInfo)){
            JOptionPane.showMessageDialog(this, "添加成功");
            selectStudent();
        }else {
            JOptionPane.showMessageDialog(this, "添加失败");
        }
    }

    private void updateStudent() {
        StudentInfo  studentInfo = new StudentInfo(); // 创建学生对象
        studentInfo.setGrade_id(Integer.parseInt(textFieldGrade_id.getText()));
        studentInfo.setClass_id(Integer.parseInt(textFieldClass_id.getText()));
        studentInfo.setStudent_id(Integer.parseInt(textFieldStudent_id.getText()));
        studentInfo.setName(textFieldName.getText());
        studentInfo.setSex(textFieldSex.getText());
        studentInfo.setCollege(textFieldCollege.getText());
        studentInfo.setMajor(textFieldMajor.getText());
        studentInfo.setIntime(Integer.parseInt(textFieldIntime.getText()));
        studentInfo.setId_num(textFieldId_num.getText());
        studentInfo.setStatus(textFieldStatus.getText());
        boolean flag = StudentDao.update(studentInfo);
        if (flag) {
            JOptionPane.showMessageDialog(this, "修改成功");
            selectStudent();
            reset();
        }else {
            JOptionPane.showMessageDialog(this, "修改失败");
        }
    }


    private void deleteStudent() {
        int student_id = Integer.parseInt(textFieldStudent_id.getText());
        if (StudentDao.delete(student_id)) {
            JOptionPane.showMessageDialog(this, "删除成功");
            selectStudent();
            reset();
        }else {
            JOptionPane.showMessageDialog(this, "删除失败");
        }
    }

    private void reset() {
        textFieldGrade_id.setText("");
        textFieldClass_id.setText("");
        textFieldStudent_id.setText("");
        textFieldName.setText("");
        textFieldSex.setText("");
        textFieldCollege.setText("");
        textFieldMajor.setText("");
        textFieldIntime.setText("");
        textFieldId_num.setText("");
        textFieldStatus.setText("");
    }

    private void selectStudent()  {
        List<StudentInfo> studentInfoList = studentDao.getAll();
        vector.clear();
        for (StudentInfo  studentInfo : studentInfoList ) {
            Vector v = new Vector();
            v.add(studentInfo.getGrade_id());
            v.add(studentInfo.getClass_id());
            v.add(studentInfo.getStudent_id());
            v.add(studentInfo.getName());
            v.add(studentInfo.getSex());
            v.add(studentInfo.getCollege());
            v.add(studentInfo.getMajor());
            v.add(studentInfo.getIntime());
            v.add(studentInfo.getId_num());
            v.add(studentInfo.getStatus());
            vector.add(v);
        }
        tableModel.fireTableDataChanged();
    }

    private void createTable() {
        JTable table = new JTable(); // 创建表格
        scrollPane.setViewportView(table); // 将表格放入滚动面板中

        // 启用横向滚动条（推荐放在 JScrollPane 初始化时）
        scrollPane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);

        vector = new Vector();
        tableModel = new AbstractTableModel() {
            @Override
            public int getRowCount() {
                return vector.size();
            }

            @Override
            public String getColumnName(int column) {
                return columnNames[column];
            }

            @Override
            public int getColumnCount() {
                return columnNames.length;
            }

            @Override
            public Object getValueAt(int rowIndex, int columnIndex) {
                if (vector.size() > 0) {
                    Vector v = (Vector) vector.get(rowIndex);
                    return v.get(columnIndex);
                } else {
                    return null;
                }
            }
        };
        table.setModel(tableModel);

        // 设置各列宽度
        TableColumn column;
        for (int i = 0; i < table.getColumnCount(); i++) {
            column = table.getColumnModel().getColumn(i);
            switch (i) {
                    case 0: // 年级id
                        column.setPreferredWidth(70);
                        break;
                    case 1: // 班级id
                        column.setPreferredWidth(70);
                        break;
                    case 2: // 学号
                        column.setPreferredWidth(70);
                        break;
                    case 3: // 姓名
                        column.setPreferredWidth(70);
                        break;
                    case 4: // 性别
                        column.setPreferredWidth(70);
                        break;
                    case 5: // 学院
                        column.setPreferredWidth(70);
                        break;
                    case 6: // 专业
                        column.setPreferredWidth(70);
                        break;
                    case 7: // 入学日期
                        column.setPreferredWidth(70);
                        break;
                    case 8: // 身份证
                        column.setPreferredWidth(70);
                        break;
                    case 9: // 状态
                        column.setPreferredWidth(70);
                        break;
            }
        }
        // ✅ 禁止自动调整列宽，这样超过窗口宽度时会出现横向滚动条（放到循环外面！）
        table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
    }
    public static void main(String[] args) {
        new StudentInfoUI().setVisible(true);
    }


}
